package com.color.admin.system.controller;


import com.color.admin.common.core.base.BaseController;
import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.page.PageRequest;
import com.color.admin.common.core.result.ResultData;
import com.color.admin.common.logger.annotation.Log;
import com.color.admin.system.entity.BaseEntity;
import com.color.admin.system.entity.SysMenu;
import com.color.admin.system.entity.query.MenuQueryCriteria;
import com.color.admin.system.servie.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/12/14
 */
@Tag(name = "menu-controller",description = "菜单按钮权限操作接口")
@RestController
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Log("菜单新增")
    @Operation(summary = "菜单新增",description = "菜单权限的新增方法接口")
    @PostMapping
    public ResultData save(@RequestBody @Validated SysMenu sysMenu){
        menuService.save(sysMenu);
        return decide(Constant.INSERT);
    }

    @Log("菜单修改")
    @Operation(summary = "菜单修改",description = "菜单权限修改接口")
    @PutMapping
    public ResultData update(@RequestBody @Validated(BaseEntity.update.class) SysMenu sysMenu){
        menuService.update(sysMenu);
       return decide(Constant.UPDATE);
    }

    @Log("菜单查询")
    @Operation(summary = "菜单查询",description = "菜单权限根据ID查询接口")
    @GetMapping
    public ResultData get(@RequestParam String menuId){
        return decide(menuService.get(menuId));
    }

    @Log("所有菜单")
    @Operation(summary = "所有菜单",description = "菜单权限所有查询接口")
    @GetMapping("/all")
    public ResultData menuAll(){
        return decide(menuService.menuAll());
    }

    @Log("树形菜单")
    @Operation(summary = "树形菜单",description = "所有菜单树形查询接口")
    @GetMapping("/tree")
    public ResultData tree(){
        return decide(menuService.tree());
    }

    @Log("菜单删除")
    @Operation(summary = "菜单删除",description = "菜单权限根据ID集合批量删除接口")
    @DeleteMapping
    public ResultData delete(@RequestBody Set<String> menuIds){
        menuService.delete(menuIds);
        return decide(Constant.DELETE);
    }

    @Log("菜单权限模糊分页查询")
    @Operation(summary = "分页查询",description = "菜单权限模糊分页查询接口")
    @PostMapping("/list")
    public ResultData list(@RequestBody PageRequest<MenuQueryCriteria> request){
        return decide(menuService.list(request));
    }

    @Log("登录获取菜单路由")
    @Operation(summary = "登录获取菜单路由",description = "用户登录获取对应的权限菜单")
    @GetMapping("/router")
    public ResultData getMenus(@RequestAttribute @Parameter(hidden = true) String userId){
        return decide(menuService.getMenus(userId));
    }

    @Log("查询用户菜单权限(内部feign调用)")
    @Operation(description = "查询用户菜单权限(内部feign调用)", hidden = true)
    @PostMapping("/auth")
    public ResultData authList(@Parameter(description = "内部服务调用查询用户菜单权限",hidden = true)
                               @RequestParam String userId) {
        return decide(menuService.authList(userId));
    }
}
